<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<script type="text/javascript">
			let data = [
			  {
			    key: '1',
			    name: 'a',
			    children: [
			      {key: '11', name: 'aa'},
			      {key: '12', name: 'ab'},
			      {key: '13', name: 'ac'}
			    ]
			  },
			  {
			    key: '2',
			    name: 'b',
			    children: [
			      {key: '21', name: 'ba'},
			      {key: '22', name: 'bb'},
			      {
			        key: '23',
			        name: 'bc',
			        children: [
			          {key: '231', name: 'bca'},
			          {key: '232', name: 'bcb'},
			          {
			            key: '233',
			            name: 'bcc',
			            children: [
			              {key: '2331', name: 'bcca'},
			              {key: '2332', name: 'bccb'},
			              {key: '2333', name: 'bccc'}
			            ]
			          }
			        ]
			      }
			    ]
			  },
			  {
			    key: '3',
			    name: 'c',
			    children: [
			      {key: '31', name: 'ca'},
			      {key: '32', name: 'cb'},
			      {key: '33', name: 'cc'}
			    ]
			  }
			]
			
	
			//查找自己
			function selectNode(data,node,key){
				for(let i = 0;  i< data.length; i++){
					if(data[i].key == key){
						//console.log(data[i]);
						Object.assign(node, data[i]);
					}else if(data[i].children != undefined && data[i].children.length > 0 ){
						selectNode(data[i].children,node,key);
					}
				}
			}
			
			
			//找儿子
			function reCallNode(rootNode,arr,node){
				if(rootNode.children != undefined && rootNode.children.length > 0){
					for(let i = 0; i < rootNode.children.length; i++){
						if(rootNode.children[i].key == node.key){
							arr.push(rootNode);
						}else{
							reCallNode(rootNode.children[i],arr,node);
						}
					}
				}
			}
			
			
		let node = {};
		let arr = [];
		//找自己，放入node中
		selectNode(data,node,'231');
		//找到后放入arr中
		arr.push(node);
		//找父亲，找到后放入数组，同时取出父找赵爷爷
		for(let i = 0; i < arr.length; i++){
			for(let j = 0; j < data.length; j++){
				reCallNode(data[j],arr,arr[i]);
			}
		}
		//reCallNode(data,arr,node);
		console.log(arr);
		</script>
	</head>
	<body>
		
	</body>
</html>